package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ArtGalleryApp()
        }
    }
}

@Composable
fun ArtGalleryApp() {

    val gradientBackground = Brush.verticalGradient(
        colors = listOf(
            Color(0xFFB5D3FA),
            Color(0xFFB9D2F3),
            Color(0xFFB9E1F5)
        )
    )


    val artworks = listOf(
        Artwork(R.drawable.gewen, "二创格温", "作者不详", 2021),
        Artwork(R.drawable.maoergewen, "邪恶猫猫头格温", "作者不详", 2021),
        Artwork(R.drawable.zhandougewen, "战斗格温", "作者不详", 2021),
        Artwork(R.drawable.zhizhuxiagewen, "不是哥们？你这格温她对吗？怎么还会吐蛛丝的？", "蜘蛛宇宙", 2022)
    )

    var currentIndex by remember { mutableIntStateOf(0) }


    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(gradientBackground)
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(24.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(600.dp)
                    .clip(RoundedCornerShape(12.dp))
                    .background(Color.White)
            ) {
                Image(
                    painter = painterResource(artworks[currentIndex].imageRes),
                    contentDescription = artworks[currentIndex].title,
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(16.dp)
                        .clip(RoundedCornerShape(8.dp)),
                    contentScale = ContentScale.Fit
                )
            }

            Spacer(modifier = Modifier.height(24.dp))

            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = artworks[currentIndex].title,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center,
                    color = Color(0xFF333333)
                )
                Text(
                    text = "${artworks[currentIndex].artist} (${artworks[currentIndex].year})",
                    color = Color(0xFF666666),  // 中灰色
                    modifier = Modifier.padding(top = 8.dp)
                )
            }

            Spacer(modifier = Modifier.height(32.dp))

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                Button(
                    onClick = {
                        currentIndex = if (currentIndex == 0) artworks.size - 1 else currentIndex - 1
                    },
                    modifier = Modifier.width(120.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(0xFF75BDF6)
                    )
                ) {
                    Text("上一张")
                }

                Button(
                    onClick = {
                        currentIndex = (currentIndex + 1) % artworks.size
                    },
                    modifier = Modifier.width(120.dp),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(0xFF70BDFA)
                    )
                ) {
                    Text("下一张")
                }
            }
        }
    }
}

data class Artwork(
    val imageRes: Int,
    val title: String,
    val artist: String,
    val year: Int
)